package cs3343.project.family;
import javax.swing.JOptionPane;


public class DiceController{
	
	int endMapArea = (MapController.size+4)*2-1;
	
	public void buttonPress(){
		
			  endMapArea = (MapController.size+4)*2-1;
		  
			 //init first lower bar once
			 if(!InitGame.firstInit){
				  InitGame.curPlay.setText(StartGame.character[0].getName());
				  InitGame.curSE.setText("SE: " + String.valueOf(StartGame.character[0].getEsteem()));
				  InitGame.curIndex.setText("Land:" + String.valueOf(StartGame.character[0].getMapAreaId()));
				  InitGame.diceNum.setText("Roll Dice." );
				  InitGame.firstInit = true;
			 }

			  
			  /* if(Start.character[1].getEsteem() == 0 &&
					  Start.character[2].getEsteem() == 0  &&
					  Start.character[3].getEsteem() == 0 ){
				  gameFrame.setVisible(false);
				  new EndGame();
			  }
			  */
			  
			  
			  
			  //TODO call dice class
			  //InitGame.drawCard.setText("Nothing Here.");
			  
			  int ran = (int)(6 * Math.random() + 1);
			  Dice.setDiceImage(ran);
			  
			  //delete
			  diceAction(ran);
					  /*
					  //TODO >0.6 draw card
					  //if (StartGame.character[i].getMapAreaId() % 2 == 0) {
					  if(Math.random() > 0.6){
						//drawCard.setText("Draw card.");
						int ranCard = (int)(CardController.eventCards.size()*Math.random() + 1);
						String cardDetail = CardController.eventCards.get(String.valueOf(ranCard)).description;
						JOptionPane.showMessageDialog(null, cardDetail, "Message", 1);
						new CardFrame(CardController.drawCard("event"), StartGame.character[i]);
						//drawCard.setText("Your Self-Esteem:" + String.valueOf(Start.character[1].esteem));
						InitGame.curSE.setText("SE: "+ String.valueOf(StartGame.character[i].getEsteem()));
						//es1.setText(String.valueOf(Start.character[1].getEsteem()));
					  }
					  InitGame.icon[i].setBounds(InitGame.map[StartGame.character[i].getMapAreaId()].getX(), InitGame.map[StartGame.character[i].getMapAreaId()].getY(), 100, 100);
					  */


			  //Check end game
			  
			  isEndGame();
			 
 
			  //next player
			  InitGame.curPlayer++;
			  
			  //loop
			  if(InitGame.curPlayer == StartGame.selectedPlayer)
				  InitGame.curPlayer = 0;
  
			  //player panel image
			  for(int i = 0; i < StartGame.selectedPlayer; i++){
				  if(InitGame.curPlayer == i){
					  InitGame.img[i].setVisible(true);
					  InitGame.curSE.setText("SE: " + String.valueOf(StartGame.character[i].getEsteem()));
					  InitGame.curPlay.setText(StartGame.character[i].getName());
					  InitGame.curIndex.setText("Land " + String.valueOf(StartGame.character[i].getMapAreaId()));
					  //InitGame.diceNum.setText("Step:" + String.valueOf(ran));
					  
					  //update card dcok
					  InitGame.drawCard.removeAllItems();
					  for(int j=0;j<StartGame.character[i].cardsOwned.size();j++)
						  InitGame.drawCard.addItem(StartGame.character[i].cardsOwned.get(j).description);

				  }
				  else
			  		InitGame.img[i].setVisible(false);		  
			  }
			  			  

			  /*
			  if (InitGame.curPlayer == 1) {
				  if (StartGame.character[1].getMapAreaId() < 19)
					  StartGame.character[1].setMapAreaId(StartGame.character[1].getMapAreaId() + ran);
				  else
					  StartGame.character[1].setMapAreaId(0);  
				  //System.out.println(map[character.getMapAreaId()].getX() +" "+ map[character.getMapAreaId()].getY());
					
				  InitGame.curPlay.setText(StartGame.character[1].getName());
				  InitGame.curIndex.setText("Land:" + String.valueOf(StartGame.character[1].getMapAreaId()));
				  InitGame.diceNum.setText("Step:" + String.valueOf(ran));
					
					if (StartGame.character[1].getMapAreaId() % 2 == 0) {
						//drawCard.setText("Draw card.");
						int ranCard = (int)(CardController.eventCards.size()*Math.random() + 1);
						String cardDetail = CardController.eventCards.get(String.valueOf(ranCard)).description;
						JOptionPane.showMessageDialog(null, cardDetail, "Message", 1);
						new CardFrame(CardController.drawCard("event"), StartGame.character[1]);
						//drawCard.setText("Your Self-Esteem:" + String.valueOf(Start.character[1].esteem));
						InitGame.curSE.setText("SE: "+ String.valueOf(StartGame.character[1].getEsteem()));
						//es1.setText(String.valueOf(Start.character[1].getEsteem()));
					}
				
			  }
			  
			  if (InitGame.curPlayer == 2) {
				  if(StartGame.character[2].getMapAreaId() < 19)
					  StartGame.character[2].setMapAreaId(StartGame.character[2].getMapAreaId() + ran);
				  else
					  StartGame.character[2].setMapAreaId(0);
			  
				  InitGame.curPlay.setText(StartGame.character[2].getName());
				  InitGame.curIndex.setText("Land:" + String.valueOf(StartGame.character[2].getMapAreaId()));
				  InitGame.diceNum.setText("Step:" + String.valueOf(ran));
					
					if (StartGame.character[2].getMapAreaId() % 2 == 0) {
						//drawCard.setText("Draw card.");
						int ranCard = (int)(CardController.eventCards.size()*Math.random() + 1);
						String cardDetail = CardController.eventCards.get(String.valueOf(ranCard)).description;
						JOptionPane.showMessageDialog(null, cardDetail, "Message", 1);
						new CardFrame(CardController.drawCard("event"), StartGame.character[2]);
						//drawCard.setText(String.valueOf(Start.character[2].esteem));
						//drawCard.setText("SE:" + String.valueOf(Start.character[2].esteem));
						InitGame.curSE.setText("SE: "+ String.valueOf(StartGame.character[2].getEsteem()));
						//es2.setText(String.valueOf(Start.character[2].getEsteem()));
					}
			  }
			  
			  if (InitGame.curPlayer == 3) {
				  if (StartGame.character[3].getMapAreaId() < 19) {
					  StartGame.character[3].setMapAreaId(StartGame.character[3].getMapAreaId() + ran);
					  InitGame.img[].setVisible(true);
					  InitGame.img2.setVisible(false);
					  InitGame.img3.setVisible(false);
				  }
				  else
					  StartGame.character[3].setMapAreaId(0);  
				  
				  InitGame.curPlay.setText(StartGame.character[3].getName());
				  InitGame.curIndex.setText("Land:"+ String.valueOf(StartGame.character[3].getMapAreaId()));
				  InitGame.diceNum.setText("Step:" + String.valueOf(ran));
					
					if(StartGame.character[3].getMapAreaId() % 2 == 0){
						//drawCard.setText("Draw card.");
						int ranCard = (int)(CardController.eventCards.size()*Math.random() + 1);
						String cardDetail = CardController.eventCards.get(String.valueOf(ranCard)).description;
						JOptionPane.showMessageDialog(null, cardDetail, "Message", 1);
						new CardFrame(CardController.drawCard("event"), StartGame.character[3]);
						//drawCard.setText(String.valueOf(Start.character[3].esteem));
						//drawCard.setText("SE:" + String.valueOf(Start.character[3].esteem));
						InitGame.curSE.setText("SE: "+ String.valueOf(StartGame.character[3].getEsteem()));
						//es3.setText(String.valueOf(Start.character[3].getEsteem()));
					}
			  }

			  
			  //move char
			  InitGame.icon1.setBounds(InitGame.map[StartGame.character[1].getMapAreaId()].getX(), InitGame.map[StartGame.character[1].getMapAreaId()].getY(), 100, 100);
			  InitGame.icon2.setBounds(InitGame.map[StartGame.character[2].getMapAreaId()].getX(), InitGame.map[StartGame.character[2].getMapAreaId()].getY(), 100, 100);
			  InitGame.icon3.setBounds(InitGame.map[StartGame.character[3].getMapAreaId()].getX(), InitGame.map[StartGame.character[3].getMapAreaId()].getY(), 100, 100);
			  			  
			  
			  //next player
			  InitGame.curPlayer++;

			  //player panel image
			  switch(InitGame.curPlayer) {
			  	case 1:
			  		InitGame.img1.setVisible(true);
			  		InitGame.img2.setVisible(false);
			  		InitGame.img3.setVisible(false);
			  		break;
			  	case 2:
			  		InitGame.img1.setVisible(false);
			  		InitGame.img2.setVisible(true);
			  		InitGame.img3.setVisible(false);
			  		break;
			  	case 3:
			  		InitGame.img1.setVisible(false);
			  		InitGame.img2.setVisible(false);
			  		InitGame.img3.setVisible(true);
			  		break;
			  }*/
		  }
	
	public void isEndGame(){
		 
		for(int i = 0, esteemCounter = 0; i < StartGame.selectedPlayer; i++){
			  
			  if(StartGame.character[i].esteem >= 1000)
				  esteemCounter++;
			  
			  if(esteemCounter == StartGame.selectedPlayer){
			  InitGame.gameFrame.setVisible(false);
			  new EndGame();
			  }
		  }
		
	}
	public void diceAction(int ran){
		for(int i = 0; i < StartGame.selectedPlayer; i++){
			  
			  /*if(i == InitGame.curPlayer){
				  if (StartGame.character[i].getMapAreaId() < endMapArea)
					  StartGame.character[i].setMapAreaId(StartGame.character[1].getMapAreaId() + ran);
				  else
					  StartGame.character[i].setMapAreaId(0);  
			*/
			  if(i == InitGame.curPlayer){
				  if (StartGame.character[i].getMapAreaId() + ran <= endMapArea){
					  StartGame.character[i].setMapAreaId(StartGame.character[i].getMapAreaId() + ran);
				  }
				  else{
					  StartGame.character[i].setMapAreaId(StartGame.character[i].getMapAreaId() + ran - endMapArea -1);
				  }
			  
				  //System.out.println(map[character.getMapAreaId()].getX() +" "+ map[character.getMapAreaId()].getY());
					
				  //InitGame.drawCard.repaint();
				  //InitGame.drawCard.updateUI();
				  
				  
				  //TODO >0.6 draw card
				  if(Math.random() > 0){
					int ranCardType = (int)(4*Math.random()+1);
					//int ranCardType = 3;
					Card ranCard = CardController.drawCard(ranCardType);
					  
					//String cardDetail = ranCard.description;
					
					//TODO move to card controller?
					if(ranCardType == 1){
						//JOptionPane.showMessageDialog(null, cardDetail, "Message", 1);
						new EventCardFrame(ranCard, StartGame.character[i]);
					}
					else if(ranCardType == 2){
						new ToolCardFrame(StartGame.character[i], ranCard, "ToolCard" );
					}
					else{
						switch(ranCardType){
						case 3: new YesNoFrame(StartGame.character[i], ranCard, "ActionCard");break;
						case 4: new YesNoFrame(StartGame.character[i], ranCard, "OralCard");break;
						}
						
					}
					  //update center self-esteem point 
					//TODO eventcard's long delay need to do esteem update inside EventCardFrame 
					  if(ranCardType != 1)
						  InitGame.se[InitGame.curPlayer].setText(String.valueOf(StartGame.character[InitGame.curPlayer].getEsteem()));
					  
					  InitGame.icon[i].setBounds(InitGame.map[StartGame.character[i].getMapAreaId()].getX(), InitGame.map[StartGame.character[i].getMapAreaId()].getY(), 100, 100);
					
				  }
			  }
		 	}
	}
}
